/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author Administrator
 */
public class MainCanvas extends Canvas implements Runnable, IMessageListenner {

    public static final int LOGIN_SCENE = 0;
    public static final int CHOOSE_GAME_SCENE = 1;
    public static final int CHOOSE_ROOM_SCENE = 2;
    public static final int CHOOSE_TABLE_SCENE = 3;
    public static final int WAITING_SCENE = 4;
    public static final int TIEN_LEN_SCENE = 5;
    public static final int BA_CAY_SCENE = 6;
    public static final int PHOM_SCENE = 7;
    public static final int CO_VUA_SCENE = 8;
    public static final int TOP_USER_SCENE = 9;
    public static final int LIST_FRIEND_SCENE = 10;
    public static final int USER_INFO_SCENE = 11;
    public static final int SHOP_SCENE = 12;
    public static final int LIST_MESSAGE = 13;
    private static final int TIME_RENDER = 35;
    private Main main;
    private boolean isRunning;
    private Scene currentScene;
    private boolean isConnected;
    private Scene preScene;
    private Scene newScene;

    public MainCanvas(Main main) {
        this.main = main;
        this.setFullScreenMode(true);
        initValue();
    }

    private void initValue() {
    }

    protected void initObject(int id) {
        if (Debug.isTest) {
            currentScene = getScene(CHOOSE_GAME_SCENE);
            return;
        }
        if (id == 0) {
            currentScene = getScene(LOGIN_SCENE);
        } else {
            currentScene = getScene(CHOOSE_GAME_SCENE);
        }
        Request.setMessageListenner(this);
    }

    public boolean initConnection() {
        Request.initializeConnection();
        Request.setMessageListenner(this);
        isConnected = Request.isConnect();
        return isConnected;
    }

    public void start() {
        isRunning = true;
        new Thread(this).start();
    }

    protected void paint(Graphics g) {
        try {
            if (currentScene.isShow) {
                currentScene.paint(g);
            }

        } catch (Exception ex) {
        }
    }

    public void run() {
        try {
            while (isRunning) {
                if (preScene != null) {
                    if (!preScene.isShow) {
                        preScene.dispose();
                        preScene = null;
                    }
                }

                if (currentScene.isShow) {
                    currentScene.doThreadAction();
                } else {
                    Debug.d("show new scene");
                    preScene = currentScene;
                    currentScene = newScene;
                    newScene = null;
                    repaint();
                }

                try {
                    Thread.sleep(TIME_RENDER);
                } catch (InterruptedException ex) {
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    protected void displayLuckyCoin() {
        isRunning = false;
        main.displayLuckyCoin();
        main.disposeMainCanvas();
    }

    protected void keyPressed(int keyCode) {
        switch (keyCode) {
            case -6:
                currentScene.doSoftKeyLeft();
                return;
            case -7:
            case 8:
                currentScene.doSoftKeyRight();
                return;
            case -5:
            case 10:
                currentScene.doKeyCenter();
                return;
            case -1:
                currentScene.doKeyUp();
                return;
            case -2:
                currentScene.doKeyDown();
                return;
            case -3:
                currentScene.doKeyLeft();
                return;
            case -4:
                currentScene.doKeyRight();
                return;
            case 35:
                //dau #
                currentScene.doKeyPound();
                return;
        }
        currentScene.inputKeyCode(keyCode);
    }

    protected void pointerDragged(int x, int y) {
        currentScene.doTouchDraged(x, y);
    }

    protected void pointerReleased(int x, int y) {
        currentScene.doTouchReleased(x, y);
    }

    protected void pointerPressed(int x, int y) {
        currentScene.doTouchPressed(x, y);
    }

    //-------------------------------------------
    private Scene getScene(int index) {
        switch (index) {
            case LOGIN_SCENE:
                return new LoginScene(this);
            case CHOOSE_GAME_SCENE:
                return new ChooseGameScene(this);
            case CHOOSE_ROOM_SCENE:
                return new ChooseRoomScene(this);
            case CHOOSE_TABLE_SCENE:
                return new ChooseTableScene(this);
            case WAITING_SCENE:
                return new WaitingRoomScene(this);
            case TIEN_LEN_SCENE:
                return new TienlenScene(this);
            case TOP_USER_SCENE:
                return new TopUserScene(this);
            case LIST_FRIEND_SCENE:
                return new ListFriendScene(this);
//            case USER_INFO_SCENE:
//                return new UserInfoScene(this);
            case SHOP_SCENE:
                return new ShopScene(this);
            case PHOM_SCENE:
                return new PhomScene(this);
            case CO_VUA_SCENE:
                return new ChessScene(this);
            case BA_CAY_SCENE:
                return new BacayScene(this);
//            case LIST_MESSAGE:
//                return new ListMessageScene(this);
        }
        return null;
    }

    public void exit() {
        main.destroyApp(true);
    }

    public void processMessage(String[] part) {
        currentScene.processMessage(part);
    }

    public void setDialogString(String message) {
    }

    public void switchScene(int index) {
        Debug.d("switch scene");
        newScene = getScene(index);
        currentScene.isSwitchScene = true;
        repaint();
    }

    void makeCall() {
        main.call("1900561588");
    }

    void openWeb(String link) {
        main.openWeb(link);
    }

    protected void sizeChanged(int w, int h) {
        currentScene.onSizeChange(w, h);
    }

    void disposeObject() {
        if (currentScene != null) {
            currentScene.dispose();
            currentScene = null;
        }
        if (preScene != null) {
            preScene.dispose();
            preScene = null;
        }
        if (newScene != null) {
            newScene.dispose();
            newScene = null;
        }
    }
}
